package com.hjc.service.impl;

import com.hjc.entity.InStore;
import com.hjc.entity.Result;
import com.hjc.mapper.InStoreMapper;
import com.hjc.mapper.ProductMapper;
import com.hjc.page.Page;
import com.hjc.service.InStoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class InStoreServiceImpl implements InStoreService {
    @Autowired
    private InStoreMapper inStoreMapper;
    @Autowired
    private ProductMapper productMapper;

    @Transactional
    @Override
    public Result addInStore(InStore inStore,Integer buyId) {
        int flag1 = inStoreMapper.insertInStore(inStore);
        if (flag1 > 0) {
            int flag2 = inStoreMapper.updatePurchaseById(buyId);
            if (flag2 > 0) {
                return Result.ok("新增入库单成功！");
            }
            return Result.err(Result.CODE_ERR_BUSINESS, "新增入库单失败！");
        }
        return Result.err(Result.CODE_ERR_BUSINESS, "新增入库单失败！");
    }

    @Override
    public Page findInStoreByPage(InStore inStore, Page page) {
        int count = inStoreMapper.selectInStoreCount(inStore);
        List<InStore> inStoreList = inStoreMapper.selectInStoreByPage(inStore, page);

        page.setTotalNum(count);
        page.setResultList(inStoreList);

        return page;
    }

    @Transactional
    @Override
    public Result confirmInStore(InStore inStore) {
        int flag1 = inStoreMapper.updateInStoreIsIn(inStore.getInsId());
        if (flag1 > 0) {
            int flag2 = productMapper.updateProductInvent(inStore.getProductId(), inStore.getInNum());
            if (flag2 > 0) {
                return Result.ok("入库成功！");
            }
            return Result.err(Result.CODE_ERR_BUSINESS, "入库失败！");
        }
        return Result.err(Result.CODE_ERR_BUSINESS, "入库失败！");
    }

}
